<template>
  <div class="app-container">
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" icon="el-icon-plus" size="mini" @click="handleAdd"
          v-hasPermi="['module:pay:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate"
          v-hasPermi="['module:pay:edit']">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"
          v-hasPermi="['module:pay:remove']">删除</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList" />
    </el-row>

    <el-table v-loading="loading" :data="payList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="第三方平台名称" align="center" prop="platformName" width="300px"/>
      <el-table-column label="商户名称" align="center" prop="merName" width="260px"/>
      <el-table-column label="商户号" align="center" prop="merNo" width="150px"/>
      <el-table-column label="终端号" align="center" prop="terminalNo" width="150px"/>
      <el-table-column label="状态" align="center">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.state == 1" :type="''">正常</el-tag>
          <el-tag v-if="scope.row.state == 0" :type="''">停用</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="是否默认" align="center">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.isDefault == 1" :type="'success'">默认</el-tag>
          <el-tag v-if="scope.row.isDefault == 0" :type="''">非默认</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['module:pay:edit']">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
            v-hasPermi="['module:pay:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <!-- 添加或修改银行支付列对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="560px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="平台名称" prop="platformName">
          <el-input v-model="form.platformName" placeholder="请输入第三方平台名称" size="mini"/>
        </el-form-item>
        <el-form-item label="商户名称" prop="merName">
          <el-input v-model="form.merName" placeholder="请输入商户名称" :disabled="form.id!=undefined" size="mini"/>
        </el-form-item>
        <el-form-item label="商户号" prop="merNo">
          <el-input v-model="form.merNo" placeholder="请输入商户号" :disabled="form.id!=undefined" size="mini"/>
        </el-form-item>
        <el-form-item label="终端号" prop="terminalNo">
          <el-input v-model="form.terminalNo" placeholder="请输入终端号" :disabled="form.id!=undefined" size="mini"/>
        </el-form-item>
        <el-form-item label="API key " prop="apiKey">
          <el-input v-model="form.apiKey" placeholder="请输入API key " :disabled="form.id!=undefined" size="mini"/>
        </el-form-item>
        <el-form-item label="开发者ID" prop="openId">
          <el-input v-model="form.openId" placeholder="请输入开发者ID" :disabled="form.id!=undefined" size="mini"/>
        </el-form-item>
        <el-form-item label="开发者key" prop="openKey">
          <el-input v-model="form.openKey" placeholder="请输入开发者key" :disabled="form.id!=undefined" size="mini"/>
        </el-form-item>
        <el-form-item label="来源编号" prop="fromCode">
          <el-input v-model="form.fromCode" placeholder="请输入来源编号" :disabled="form.id!=undefined" size="mini"/>
        </el-form-item>
        <el-form-item label="系统编号" prop="sysCode">
          <el-input v-model="form.sysCode" placeholder="请输入系统编号" :disabled="form.id!=undefined" size="mini"/>
        </el-form-item>
        <el-form-item label="状态" prop="state" >
          <el-radio-group v-model="form.state" size="mini">
            <el-radio :value="0" :label="0">停用</el-radio>
            <el-radio :value="1" :label="1">正常</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button :loading="buttonLoading" size="mini" type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel" size="mini">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listPay, getPay, delPay, addPay, updatePay, exportPay } from "@/api/module/pay/pay";
import {listEstate} from "@/api/module/estate/estate";

export default {
  name: "Pay",
  data() {
    return {
      // 按钮loading
      buttonLoading: false,
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 银行支付列表格数据
      payList: [],
      // 小区列表
      estateList:[],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
      },
      // 表单参数
      form: {},

    }
  },
  created() {
    this.getList();
    this.getEstateChildOptionList();
  },
  methods: {
    /** 查询银行支付列列表 */
    getList() {
      this.loading = true;
      listPay(this.queryParams).then(response => {
        this.payList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 查询小区表
    getEstateChildOptionList(){
      listEstate(this.queryParamsPty).then(response => {
        this.estateList = response.rows;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        platformName: undefined,
        merName: undefined,
        merNo: undefined,
        terminalNo: undefined,
        apiKey: undefined,
        openId: undefined,
        openKey: undefined,
        fromCode: undefined,
        sysCode: undefined,
        clientId: undefined,
        privateKey: undefined,
        publicKey: undefined,
        payLinkUrl: undefined,
        searchLinkUrl: undefined,
        refundLinkUrl: undefined,
        cancelLinkUrl: undefined,
        setttleSearchLinkUrl: undefined,
        settleDownLinkUrl: undefined,
        adminLink: undefined,
        adminUserName: undefined,
        adminPassword: undefined,
        state: undefined,
        isDefault: undefined,
        query:undefined,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.reset("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加银行支付列";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.loading = true;
      this.reset();
      const id = row.id || this.ids;
      getPay(id).then(response => {
        this.loading = false;
        this.form = response.data;
        this.open = true;
        this.title = "修改银行支付列";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.buttonLoading = true;
          if (this.form.id != null) {
            updatePay(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            }).finally(() => {
              this.buttonLoading = false;
            });
          } else {
            addPay(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            }).finally(() => {
              this.buttonLoading = false;
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除编号为"' + ids + '"的数据项?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(() => {
        this.loading = true;
        return delPay(ids);
      }).then(() => {
        this.loading = false;
        this.getList();
        this.msgSuccess("删除成功");
      }).catch(() => {});
    },
  }
}
</script>
